package com.idea.errorcode.api;

import com.idea.errorcode.messages.resolver.CachedMessageResolver;

/**
 * @author: 沉香
 * @date: 2025/04/13
 * @description: 异常构建工具类
 */
public class ErrorBuilder {

    private final IErrorCode code;

    private Object[] args;

    private ErrorBuilder(IErrorCode code) {
        this.code = code;
    }

    public static ErrorBuilder withCode(IErrorCode code) {
        return new ErrorBuilder(code);
    }

    public ErrorBuilder args(Object... args) {
        this.args = args;
        return this;
    }


    public <R extends RuntimeException> void throwAs(TriFunction<String, String, Integer, R> exceptionMapper) {
        throw exceptionMapper.apply(code.getErrorCode(), CachedMessageResolver.resolve(code, args), code.getCode());
    }


}